package com.dubbo.cp.consumer.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.RoleService;
import com.dubbo.cp.entity.RoleEntity;
import com.dubbo.cp.entity.UserEntity;
import com.dubbo.cp.uitls.Pager;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
	@Reference
	private RoleService roleServiceImpl;
	private Pager<RoleEntity> pager;

	@RequestMapping(value="uList")
	public String roleList(Model model) {


		List<RoleEntity> useList = roleServiceImpl.getRoleList();
		model.addAttribute("useList", useList);
		System.out.println("useList"+useList);

		return "roleList";
	}
	/*
	 * 
	 * 分页查询用户信息
	 */
	@RequestMapping(value="findRoleByPage")
	public ModelAndView findByPage(Model model,String currentPage) {
		System.out.println("进入分页查询");
		int pageSize = Integer.parseInt(currentPage);
		if(pager==null) {
			pager = new Pager<RoleEntity>();
		}
		pager.setCurrentPage(pageSize);
		int length = pager.getCurrentPage();
		int recordSize = pager.getRecordSize();
		int start = (length-1)*recordSize;
		pager.setList(roleServiceImpl.getRoleList());
		pager.initTotalPage();
		List<RoleEntity>ulist = roleServiceImpl.getRoleByPage(start, pager.getRecordSize());
		model.addAttribute("ulist", ulist);
		model.addAttribute("pager", pager);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/roleList");
		return modelAndView;

	}


	/**
	 * 
	 * 
	 * 添加角色跳转
	 * @return
	 */
	@RequestMapping(value="inputRole")
	public String inputRole() {
		return "roleAdd";

	}
	
	@PostMapping(value="insertRole")
	public String insertRole(@Valid RoleEntity roleEntity) {

		System.out.println("添加");
		System.out.println(roleEntity.getRole());
		roleServiceImpl.insertRoleEntity(roleEntity);

		return "redirect:findRoleByPage?currentPage=1";

	}
	 /**
     * 编辑跳转
     * @return
     */
	
	@RequestMapping(value="editRole")
	public ModelAndView editRole(Model model,@RequestParam int id) {
		
		System.out.println("id===="+id);
		
		RoleEntity roleEntity = roleServiceImpl.findRoleByRoleid(id);
		model.addAttribute("roleEntity",roleEntity);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/roleEdit");
		return modelAndView;
		
	}
	
	@PostMapping(value="updateRole")
	public String updateRole(RoleEntity roleEntity) {
		roleServiceImpl.updateRoleEntity(roleEntity);
		
		return "redirect:findRoleByPage?currentPage=1";
		
	}
	
	@RequestMapping(value="deleteRole")
	public String deleteRole(@RequestParam int id) {
		System.out.println(id);
		roleServiceImpl.deleteRoleEntity(id);
		return "redirect:findRoleByPage?currentPage=1";
		
	}
}
